<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:ConfTest.php,2010/05/13 14:10:13 
 * 
 **************************************************************************/
 
 
 
/**
 * @file ConfTest.php
 * @author huqingping
 * @date 2010/05/13 14:10:13
 * @version 1.0 
 * @brief 
 *  
 **/

require_once 'apiproxy/ApiProxy.php';

final class ApiProxyTest extends PHPUnit_Framework_TestCase
{
	function testGetProxy()
	{
		ApiProxy::init(
			array(
				'mod'=>array(
					'yy'=>array('class'=>'RpcProxy','mod'=>'yy2','server'=>''),
					'zz'=>array('class'=>'HttpRpcProxy','mod'=>'zz2','server'=>'')
				),
				'servers'=>array(
				)
			),
			array('app_root'=>dirname(__FILE__).'/','conf_root'=>dirname(__FILE__).'/')
		);
		$proxy = ApiProxy::getProxy('mod');
		$proxy = ApiProxy::getProxy('yy');
		$proxy = ApiProxy::getProxy('zz');
	}

	function testProxyMethod()
	{
		ApiProxy::init(
			array(
				'mod'=>array(
					'yy'=>array('class'=>'RpcProxy','server'=>''),
					'zz'=>array('class'=>'HttpRpcProxy','mod'=>'zz2','server'=>'')
				),
				'servers'=>array(
				)
			),
			array('app_root'=>dirname(__FILE__).'/','conf_root'=>dirname(__FILE__).'/')
		);
		$proxy = ApiProxy::getProxy('mod');
		$this->assertEquals('12', $proxy->concat(1,2));
		$proxy = ApiProxy::getProxy('mod','aa');
		$this->assertEquals('aa12', $proxy->concat(1,2));
		$proxy = ApiProxy::getProxy('yy');
		$this->assertEquals('concat2', $proxy->concat(array(1,2)));
		$proxy = ApiProxy::getProxy('zz');
		$this->assertEquals('/_private/rpc/zz2.concat20',$proxy->concat(2,3));
		$this->assertEquals('url2',$proxy->_fetch('url', array(2,3)));
	}

	function testIntercepter()
	{
		$proxy = ApiProxy::getProxy('mod');
		$intercepter = new TransactionIntercepter();
		$proxy->addIntercepter($intercepter);
		$this->assertEquals(1, count($proxy->getIntercepters()));
		$proxy->removeIntercepter($intercepter);
		$this->assertEquals(0, count($proxy->getIntercepters()));
		$proxy->addIntercepter($intercepter);
		$this->assertEquals(1, count($proxy->getIntercepters()));
		$this->assertEquals('23',$proxy->concat(2,3));
	}

	function testTrx()
	{
		$proxy = ApiProxy::getProxy('mod');
		$proxy->enableTransaction();
	}

	function testOther()
	{
		ApiProxy::registerProxy(new MyProxy('mod1'));
		$proxy = ApiProxy::getProxy('mod1');
		$this->assertEquals('test', $proxy->test());
	}
}


class MyProxy extends BaseProxy
{
	function __construct($mod)
	{
		parent::__construct($mod);
	}

	function init($conf,$params)
	{
	}

	function call($name,$args)
	{
		return $name;
	}
}

class TransactionIntercepter implements IIntercepter
{
	function before(IProxy $proxy,$name,$args)
	{
	}

	function after(IProxy $proxy,$name,$args,$ret)
	{
	}

	function exception(IProxy $proxy,$name,$args)
	{
	}
}



/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
